package kotlinquanweizhinan.chapt05匿名函数与函数类型.函数类型作为返回值

class SimVillage {
}

fun main(args:Array<String>) {
    runSimulation()
}


fun runSimulation(){
    val greetingFunction = configureGreetingFunction()
    println(greetingFunction("Guyal"))
    println(greetingFunction("Guyal"))
}

/**
 * 为GreetingFunction变量的lambda配置参数，产生并返回lambda待用
 * 把configureGreetingFunction函数看作"函数工厂"，即配置产生函数的函数。
 * 该函数声明了必要的变量并在lambda中使用，然后把lambda返回给runSimulation调用者
 */
fun configureGreetingFunction():(String) ->String{
    val structureType = "hospitals"
    var numBuildings = 5
    return { playerName:String ->
        var currentYear = 2018
        numBuildings +=1
        println("Adding $numBuildings $structureType")
        "Welcome to SimVillage,$playerName!(copyright $currentYear)"
    }
}